package chapter_27;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MainTest {

    public static void main(String[] args) {

        try {
            Class<?> aClass = Class.forName("chapter_27.Main");

            // 读取类注解信息
            if (aClass.isAnnotationPresent(MyAnnotation.class)) {
                MyAnnotation myAnnotation = (MyAnnotation) aClass.getAnnotation(MyAnnotation.class);
                System.out.printf("类: %s, 读取注解解释: %s \n", aClass.getName(), myAnnotation.description());
            }

            // 读取成员方法注解信息
            Method[] methods = aClass.getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(MemberAnnotation.class)) {
                    MemberAnnotation memberAnnotation = (MemberAnnotation) method.getAnnotation(MemberAnnotation.class);
                    System.out.printf("成员方法: %s, 读取注解解释: %s \n", method.getName(), memberAnnotation.description());
                }
            }

            // 读取成员变量注解信息
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(MemberAnnotation.class)) {
                    MemberAnnotation memberAnnotation = (MemberAnnotation) field.getAnnotation(MemberAnnotation.class);
                    System.out.printf("成员变量: %s, 读取注解解释: %s \n", field.getName(), memberAnnotation.description());
                }
            }

        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException");
        }
    }
}
